package 曾世昌12_20.demo4;

import cn.itcast.xml.XmlUtils;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class test {
    static HashMap<String, Object> map = new HashMap<>();
    static HashMap<String, Student> stu = new HashMap<>();

    public static void main(String[] args) throws Exception {
        List<Element> course = new SAXReader().read( "E:\\untitled5\\src\\rc\\demo12_20\\demo4\\course.xml" ).getRootElement().elements( "course" );
        for (Element element : course) {
            map.put( element.attributeValue( "id" ), element.elementText( "name" ) );
        }
        List<Student> students = XmlUtils.readAll( "E:\\untitled5\\src\\rc\\demo12_20\\demo4\\student.xml", Student.class );
        for (Student student : students) {
            stu.put( student.getId(), student );
        }
        System.out.println( stu );
        System.out.println( map );
        System.out.println( "=================" );
        run();
        System.out.println( "=================" );
        run1();
        System.out.println( "=================" );
        run2();
    }

    private static void run2() {
        for (Map.Entry<String, Object> Entry : map.entrySet()) {
            for (Map.Entry<String, Student> stringStudentEntry : stu.entrySet()) {
                if (Entry.getValue().equals( "羽毛球" ) && Entry.getKey().equals( stringStudentEntry.getValue().getCourse() ) && stringStudentEntry.getValue().getAge() == 18) {
                    System.out.println( stringStudentEntry.getValue().getName() );
                }
            }
        }
    }

    public static void run() {
        for (Map.Entry<String, Student> entry : stu.entrySet()) {
            for (Map.Entry<String, Object> stringObjectEntry : map.entrySet()) {
                if (stringObjectEntry.getValue().equals( "羽毛球" ) && entry.getValue().getCourse().equals( stringObjectEntry.getKey() )) {
                    System.out.println( entry.getValue().getName() );
                }
            }
        }
    }

    public static void run1() {
        String string = "";
        String stringmax = "";
        for (Map.Entry<String, Student> entry : stu.entrySet()) {
            int a = 0, max = 0;

            for (Map.Entry<String, Object> stringObjectEntry : map.entrySet()) {
                if (entry.getValue().getCourse().equals( stringObjectEntry.getKey() )) {
                    a++;
                    string = (String) stringObjectEntry.getValue();
                }
            }
            if (max < a) {
                max = a;
                stringmax = string;
            }
        }
        System.out.println( stringmax );
    }
}
